package com.clss.commandhelper.tools.janus.util;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;

import java.util.List;

public class SystemUiHiderUtil {
	public static final String TAG = "SystemUiHiderUtil"; 
	View mGestureImageView;//点击mGestureImageView（一个imageview）的上下两段区域，显示或隐藏上下两段部件。
	boolean AUTO_HIDE=true;
	boolean TOGGLE_ON_CLICK=true;

	private static final int AUTO_HIDE_DELAY_MILLIS = 5000;  //自动隐藏时间为5s
	private SystemUiHider mSystemUiHider;
	private boolean mFloatWindows = false; //是否在点击有效区域
	Activity activity;
	public SystemUiHiderUtil(Activity activity){
		this.activity=activity;
	}
	List<SystemUiHiderBean> viewList;
	public void setListView(View mGestureImageView,List<SystemUiHiderBean> viewList,int flags){
		this.mGestureImageView=mGestureImageView;
		this.viewList=viewList;
		initSystemUiHider(mGestureImageView,flags);
	}
	
	
	public void initSystemUiHider(View anchorView, int flags){
		mSystemUiHider = SystemUiHider.getInstance(activity, anchorView,flags);
		mSystemUiHider.setup();
		mSystemUiHider.setOnVisibilityChangeListener(new SystemUiHider.OnVisibilityChangeListener() {
			// Cached values.
			int mControlsHeight;
			int mControlsWidth;
			int mShortAnimTime;
			@Override
			public void onVisibilityChange(boolean visible) {
				if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
					// If the ViewPropertyAnimator API is available
					// (Honeycomb MR2 and later), use it to animate the
					// in-layout UI controls at the bottom of the
					// screen.

					
					if (mShortAnimTime == 0) {
						mShortAnimTime = activity.getResources().getInteger(
								android.R.integer.config_shortAnimTime);
					}
					/*
					//上段部件显示或隐藏动画 
					if (mControlsHeight == 0) {
						mControlsHeight = mFilesTitleBar.getHeight();
						mFilesTitleBar.getWidth();
					}
					mFilesTitleBar
					.animate()
					.translationX(visible ? 0 : -mControlsHeight)
					.setDuration(mShortAnimTime);


					//下段部件显示或隐藏动画 
					mGalleryFramelayout
					.animate()
					.translationX(visible ? 0 : 200)
					.setDuration(mShortAnimTime);*/
					
					for (int i = 0; i < viewList.size(); i++) {
						SystemUiHiderBean systemUiHiderBean=viewList.get(i);
						View view=systemUiHiderBean.getView();
						String translation=systemUiHiderBean.getTranslation();
						mControlsHeight=view.getHeight();
						mControlsWidth=view.getWidth();
						if(translation.equals("xl")){
							view.animate().translationX(visible ? 0 : -mControlsWidth)
							.setDuration(mShortAnimTime+i*100);
						}else if(translation.equals("xr")){
							view.animate().translationX(visible ? 0 : mControlsWidth)
									.setDuration(mShortAnimTime+i*100);
						}else if(translation.equals("yt")){
							view.animate().translationY(visible ? 0 : -mControlsHeight)
									.setDuration(mShortAnimTime+i*100);
						}else{
							view.animate().translationY(visible ? 0 : mControlsHeight)
							.setDuration(mShortAnimTime+i*100);
						}
					}
				} else {
					// If the ViewPropertyAnimator APIs aren't
					// available, simply show or hide the in-layout UI
					// controls.
					for (int i = 0; i < viewList.size(); i++) {
						viewList.get(i).getView().setVisibility(visible ? View.VISIBLE: View.GONE);;
					}
//					mFilesTitleBar.setVisibility(visible ? View.VISIBLE: View.GONE);
//					mGalleryFramelayout.setVisibility(visible ? View.VISIBLE: View.GONE);
				}
				if (visible && AUTO_HIDE) {
					// Schedule a hide().
					Log.i(TAG,"delayedHide     haha ");
					delayedHide(AUTO_HIDE_DELAY_MILLIS);
				}
			}
		});
		mGestureImageView.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				Log.i(TAG,"onClick");
				if(TOGGLE_ON_CLICK){
//					if (mFloatWindows) {
						mSystemUiHider.toggle();
//					} else {
//						mSystemUiHider.show();
//					}
				}
			}
		});
		delayedHide(AUTO_HIDE_DELAY_MILLIS);
	}
	Handler mHideHandler = new Handler();
	Runnable mHideRunnable = new Runnable() {
		@Override
		public void run() {
			if( null != mSystemUiHider )
			{
				mSystemUiHider.hide();
			}
		}
	};
	private void delayedHide(int delayMillis){
		mHideHandler.removeCallbacks(mHideRunnable);
		mHideHandler.postDelayed(mHideRunnable, delayMillis);
	}
	//是否在有效区域内点击
	View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() {
		@Override
		public boolean onTouch(View view, MotionEvent motionEvent) {
			if (AUTO_HIDE) {
				if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
					float x = motionEvent.getX();
					float y = motionEvent.getY();
					WindowManager wm = (WindowManager)activity.getSystemService(Context.WINDOW_SERVICE);

					int width = wm.getDefaultDisplay().getWidth();
					int height = wm.getDefaultDisplay().getHeight();
					Log.i(TAG,"width: "+width);
					Log.i(TAG,"height: "+height);
					if (y > height-200 ||  y < 100) {
						mFloatWindows = true;
					} else {
						mFloatWindows = false;
					}
				}
			}
			return false;
		}
	};
}
